package com.xhwl.data.mq.producer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;

/**
 * @Author: JCccc
 * @Description: 将账号数据推送到主题
 * @Date: Create in 17:23 2021/06/15
 */
@Component
public class RoleProducer {

    private static String DELETE_TOPIC = "cent-data-role-delete";

    @Autowired
    private KafkaTemplate<String, Object> kafkaTemplate;
    private final Logger log = LoggerFactory.getLogger(RoleProducer.class);


    public void deletePush(String jsonStr) {
        //发送消息 send 方法可以指定分区或者不指定分区
        ListenableFuture<SendResult<String, Object>> future = kafkaTemplate.send(DELETE_TOPIC, jsonStr);
        future.addCallback(new ListenableFutureCallback<SendResult<String, Object>>() {
            @Override
            //推送成功
            public void onSuccess(SendResult<String, Object> result) {
                log.info("主题={}， 角色删除数据推送成功={}", DELETE_TOPIC, jsonStr);
            }

            @Override
            //推送失败
            public void onFailure(Throwable ex) {
                log.info("主题={}， 角色删除数据推送失败={}", DELETE_TOPIC, jsonStr);
            }
        });
    }


}